package com.xspace.android.xafcommon.db.dbsub;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xspace.android.xafcommon.been.SortBean;
import com.xspace.android.xafcommon.db.BaseSql;

import java.util.ArrayList;
import java.util.List;


public class QACSortDBBiz extends BaseSql {

    private String sortTable = "sort";

    private String sortMid = "mid";
    private String sortName = "name";
    private String sortIsLeaf = "isLeaf";
    private String sortPid = "pid";
    private String sortUrl = "url";

    public QACSortDBBiz(MyDBHelper db) {
        super(db);
    }

    public QACSortDBBiz() {

    }

    @Override
    public void create(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + sortTable
                + " (_ID INTEGER PRIMARY KEY AUTOINCREMENT," + sortMid
                + " text," + sortName + " text," + sortIsLeaf + " text,"
                + sortPid + " text," + sortUrl + " text);");

    }

    public void dropTable(SQLiteDatabase db) {

        db.execSQL("DROP TABLE IF EXISTS " + sortTable);
    }

    @Override
    public void upgrade(SQLiteDatabase db) {
    }

    /**
     * 保存sort数据到本地数据库
     */
    public int saveSort(SortBean mBean) {
        where = sortMid + "='" + mBean.obid + "' and " + sortName + "='"
                + mBean.name + "' and " + sortPid + "='" + mBean.pid
                + "'";
        values = new ContentValues();

        values.put(sortMid, mBean.obid);
        values.put(sortName, mBean.name);
        values.put(sortIsLeaf, mBean.isLeaf);
        values.put(sortPid, mBean.pid);
        values.put(sortUrl, mBean.url);
        return super.saveOrUpdate(sortTable);

    }

    /**
     * 查询保存的sort信息
     */
    public List<SortBean> querySort(String pid) {
        where = "pid='" + pid + "'";
        List<SortBean> list = new ArrayList<SortBean>();
        Cursor cur = null;
        try {
            cur = db.query(sortTable, where);
            if (cur != null) {
                for (int i = 0; i < cur.getCount(); i++) {
                    if (cur.moveToPosition(i)) {
                        SortBean bean = new SortBean();
                        bean.obid = cur.getString(1);
                        bean.name = cur.getString(2);
                        bean.isLeaf = cur.getString(3);
                        bean.pid = cur.getString(4);
                        bean.url = cur.getString(5);
                        list.add(bean);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cur != null) {
                cur.close();
            }

        }

        return list;
    }

    /**
     * 查询保存的sort信息
     */
    public List<SortBean> queryUpSort(String where) {
        where = "mid='" + where + "'";
        List<SortBean> list = new ArrayList<SortBean>();

        try {
            Cursor cur = db.query(sortTable, where);
            if (cur != null) {
                for (int i = 0; i < cur.getCount(); i++) {
                    if (cur.moveToPosition(i)) {
                        SortBean bean = new SortBean();
                        bean.obid = cur.getString(1);
                        bean.name = cur.getString(2);
                        bean.isLeaf = cur.getString(3);
                        bean.pid = cur.getString(4);
                        list.add(bean);
                    }
                }
            }
            cur.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }


    /**
     * 删除保存的sort信息
     */
    public int deleteAllSort() {
        return super.delete(sortTable);
    }
}
